﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Controller;
namespace Web
{
    public partial class VendorDetail : System.Web.UI.Page
    {
        Helper helper = new Helper();
        VendorEntity vendorEntity = new VendorEntity();
        VendorController vendorController = new VendorController();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int vendorId = Convert.ToInt32(Request.QueryString["vendorId"]);
                vendorEntity = vendorController.findVendorByVendorId(vendorId);
                NameLabel.Text = vendorEntity.Name;
                ShortNameLabel.Text = vendorEntity.ShortName;
                NumberLabel.Text = vendorEntity.Number.ToString();
                CodeLabel.Text = vendorEntity.Code;
                AddressLabel.Text = vendorEntity.Address;
                PhoneLabel.Text = vendorEntity.Phone;
                FaxLabel.Text = vendorEntity.Fax;
                EmailLabel.Text = vendorEntity.Email;
                ContactLabel.Text = vendorEntity.ContactPerson;
                ZipCodeLabel.Text = vendorEntity.ZipCode;
                RemarkLabel.Text = vendorEntity.Remark;
            }
            catch (Exception)
            {
                Session["FlashMessage"] = "Error: Invalid Vendor Id!";
                Session["FlashMessageType"] = "error";
            }
           
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            int vendorId = Convert.ToInt32(Request.QueryString["vendorId"]);
            Response.Redirect("EditVendor.aspx?vendorId=" + vendorId);
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            int vendorId = Convert.ToInt32(Request.QueryString["vendorId"]);
            bool result = vendorController.DeleteVendorById(vendorId);
            if (result == true)
            {
                Session["FlashMessage"] = "Success: Delete Customer Successful!";
                Session["FlashMessageType"] = "success";
                Response.Redirect("VendorList.aspx");
            }
            else
            {
                Session["FlashMessage"] = "Error: Delete Customer unsuccessful!";
                Session["FlashMessageType"] = "error";
            }

        }
    }
}